package cn.com.platform.web.manage.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.easy.permission.service.system.urlreserve.anotion.UrlReserveAnotion;
import cn.com.easy.permission.web.controller.BaseController;
import cn.com.easy.permission.web.dto.MessageDTO;
import cn.com.easy.utils.ResponseOutputUtils;
import cn.com.platform.dao.AreaDao;
import cn.com.platform.entity.AreaEntity;
import cn.com.platform.mybatis.BlockItemMybatisDao;
import cn.com.platform.web.manage.dto.CityTreeDto;

/**
 * 首页控制器
 * 
 * @author wxy 2016年8月19日
 */
@Controller
@RequestMapping("/manage")
public class CityManageController extends BaseController {
	private Logger logger = LoggerFactory.getLogger(CityManageController.class);

	/** 城市dao */
	@Autowired
	private AreaDao areaDao;

	/** 内容推荐MybatisDao */
	@Autowired
	private BlockItemMybatisDao blockItemMybatisDao;

	/**
	 * 获取开通省份
	 * 
	 * @param request
	 * @param response
	 * @author wxy 2016年8月16日
	 */
	@UrlReserveAnotion("获取开通省份")
	@RequestMapping(value = "/Ajax/findProvinceList")
	public void findProvinceList(HttpServletRequest request, HttpServletResponse response) {
		try {
			List<AreaEntity> dataProvinceEntities = areaDao.findByLevel(0);
			ResponseOutputUtils.renderJson(response, MessageDTO.newInstance("", true, dataProvinceEntities));
		} catch (Exception ex) {
			logger.error("获取区域异常.", ex);
			ResponseOutputUtils.renderJson(response, MessageDTO.newInstance("", false, "获取区域异常.ex:" + ex.getLocalizedMessage()));
		}
	}

	/**
	 * 获取开通城市
	 * 
	 * @param request
	 * @param response
	 * @author wxy 2016年8月16日
	 */
	@UrlReserveAnotion("获取开通城市")
	@RequestMapping(value = "/Ajax/findCityList")
	public void findCityList(HttpServletRequest request, HttpServletResponse response, Long provinceId) {
		try {
			List<AreaEntity> dataCityEntities = areaDao.findByParentId(provinceId);
			ResponseOutputUtils.renderJson(response, MessageDTO.newInstance("", true, dataCityEntities));
		} catch (Exception ex) {
			logger.error("获取城市异常.", ex);
			ResponseOutputUtils.renderJson(response, MessageDTO.newInstance("", false, "获取城市异常.ex:" + ex.getLocalizedMessage()));
		}
	}

	/**
	 * 获取开通区域
	 * 
	 * @param request
	 * @param response
	 * @author wxy 2016年8月22日
	 */
	@UrlReserveAnotion("获取开通区域")
	@RequestMapping(value = "/Ajax/findAreaList")
	public void findAreaList(HttpServletRequest request, HttpServletResponse response, Long cityId) {
		try {
			List<AreaEntity> dataAreaEntities = areaDao.findByParentId(cityId);
			ResponseOutputUtils.renderJson(response, MessageDTO.newInstance("", true, dataAreaEntities));
		} catch (Exception ex) {
			logger.error("获取区域异常.", ex);
			ResponseOutputUtils.renderJson(response, MessageDTO.newInstance("", false, "获取区域异常.ex:" + ex.getLocalizedMessage()));
		}
	}

	/***
	 * 通过城市获取省城市
	 * 
	 * @param request
	 * @param response
	 * @param cityId
	 * @author chenbinghuang 2017年6月19日
	 */
	@UrlReserveAnotion("通过城市获取省城市")
	@RequestMapping(value = "/Ajax/findCityListByCity")
	public void findCityListByCity(HttpServletRequest request, HttpServletResponse response, Long cityId) {
		try {
			// 获取城市
			AreaEntity dataCityEntity = new AreaEntity();
			List<AreaEntity> dataCityEntities = new ArrayList<AreaEntity>();
			if (cityId == -1) {
				dataCityEntity.setPinyin("qg");
				dataCityEntity.setDeleteStatus(false);
				dataCityEntity.setId((long) -1);
				dataCityEntity.setIsHot(false);
				dataCityEntity.setAudit(false);
				dataCityEntity.setLevel(0);
				dataCityEntity.setName("全国");
				dataCityEntity.setParentId((long) -1);
				dataCityEntity.setVersion((long) 1);
				dataCityEntities.add(dataCityEntity);
			} else {
				dataCityEntity = areaDao.findOne(cityId);
				dataCityEntities = areaDao.findByParentId(dataCityEntity.getParentId());
			}
			ResponseOutputUtils.renderJson(response, MessageDTO.newInstance("", true, dataCityEntities));
		} catch (Exception ex) {
			logger.error("获取城市异常.", ex);
			ResponseOutputUtils.renderJson(response, MessageDTO.newInstance("", false, "获取城市异常.ex:" + ex.getLocalizedMessage()));
		}
	}

	/**
	 * 通过区域获取同城区域
	 * 
	 * @param request
	 * @param response
	 * @author wxy 2016年8月23日
	 */
	@UrlReserveAnotion("通过区域获取同城区域")
	@RequestMapping(value = "/Ajax/findAreaListByArea")
	public void findAreaListByArea(HttpServletRequest request, HttpServletResponse response, Long areaId) {
		try {
			// 获取城市
			AreaEntity dataAreaEntity = areaDao.findOne(areaId);
			List<AreaEntity> dataAreaEntities = areaDao.findByParentId(dataAreaEntity.getParentId());
			ResponseOutputUtils.renderJson(response, MessageDTO.newInstance("", true, dataAreaEntities));
		} catch (Exception ex) {
			logger.error("获取区域异常.", ex);
			ResponseOutputUtils.renderJson(response, MessageDTO.newInstance("", false, "获取区域异常.ex:" + ex.getLocalizedMessage()));
		}
	}

	/***
	 * 获取城市树
	 * 
	 * @param request
	 * @param response
	 * @author chenbinghuang 2017年6月19日
	 */
	@UrlReserveAnotion("获取城市树")
	@RequestMapping(value = "/Ajax/findCityTree")
	public void findCityTree(HttpServletRequest request, HttpServletResponse response) {
		try {

			// 处理使用
			List<CityTreeDto> allList = new ArrayList<CityTreeDto>();
			// 给予根目录
			CityTreeDto cityTreeDto = new CityTreeDto();
			cityTreeDto.setId(0l);
			cityTreeDto.setName("城市");
			allList.add(cityTreeDto);
			// 所有城市和省份
			List<AreaEntity> areaList = areaDao.findByLevelOrLevel(0, 1);
			if (CollectionUtils.isNotEmpty(areaList) == true) {
				for (AreaEntity areaEntity : areaList) {
					cityTreeDto = new CityTreeDto();
					cityTreeDto.setId(areaEntity.getId());
					cityTreeDto.setParentId(areaEntity.getParentId());
					cityTreeDto.setName(areaEntity.getName());
					allList.add(cityTreeDto);
				}
			}
			// 增加全国推送
			CityTreeDto cityTreeDto2 = new CityTreeDto();
			cityTreeDto2.setParentId((long) -1);
			cityTreeDto2.setName("全国");
			cityTreeDto2.setId((long) -1);
			cityTreeDto2.setProvinceName("全国");
			cityTreeDto2.setCityName("全国");
			cityTreeDto2.setCityId((long) -1);
			allList.add(cityTreeDto2);
			ResponseOutputUtils.renderJson(response, MessageDTO.newInstance("", true, allList));
		} catch (Exception ex) {
			logger.error("获取推荐位异常.", ex);
			ResponseOutputUtils.renderJson(response, MessageDTO.newInstance("", false, "获取推荐位异常.ex:" + ex.getLocalizedMessage()));
		}
	}
}
